﻿<?php 
class LuyenUser_UserController extends Zend_Controller_Action
{
	protected $mailUser;	
	public function createAction ()
	{
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($defaultNamespace->name){
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}else{	$frmUser = new Form_UserForm();
			$frmUser->setAction('create');
			$frmUser->setMethod('post');
			if ($this->_request->isPost()) {			
				$mdlUser = new Model_User();
				$rowUser = $mdlUser->find($_POST['email'])->current();
				if(!$rowUser){
					if($_POST['diachi']){
						if($_POST['dienthoai']){
							if($_POST['password']){	
								if($_POST['email']){
									if($_POST['hoten']){
										if ($_POST['password'] == $_POST['re_type_password']) {
											$user=$mdlUser->createUser($_POST['hoten'],$_POST['ngaysinh'],
												$_POST['dienthoai'],$_POST['diachi'],
												$_POST['email'],$_POST['password']
											);
											$defaultNamespace->name = $_POST['hoten'];
											$defaultNamespace->user = $user;
											$messagesModel = new Model_Messages();
											$m_noidung= $messagesModel->contentEmailHeader();
											$m_dadoc = 0;
											$m_tieude ="Chào mừng bạn đến với Hệ thống shoppingcart";
											
			
											$messagesModel->createMessages($user->u_id,$m_tieude,$m_noidung,$m_dadoc);

											/******************gui mail*****************/
											$to = $user->u_email;
											$subject = "Đăng kí tài khoản tại Shoppingcart";
											$message = $messagesModel->contentMail($user->u_email,$_POST['password']);
											$from = "shoppingcartcnpm@gmail.com";
											$headers = "From: $from";
											mail($to,$subject,$message,$headers);
											//var_dump($message);											
											header('Location: http://shoppingcartcnpm.orgfree.com');
										}else{
											$this->view->message= "Password không đúng. Đánh lại password";
										}
									}else $this->view->message= "Họ tên không được để trống";
								}else $this->view->message= "Email không được để trống"; 	
							}else $this->view->message= "Password không được để trống"; 
						}else $this->view->message= "Điện thoại không được để trống";
					}else $this->view->message="Địa chỉ không được để trống";
				}else{
					$this->view->message="Email này đã đựơc đăng ký";
				}
			}
			$this->view->form = $frmUser;
		}
	}
	
	public function successAction()
	{
		
	}
	public function editAction()
	{
		Zend_Session::start();
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($defaultNamespace->name){		
			$formEdit = new Form_UserForm();
			$formEdit->removeElement('password');
			$formEdit->removeElement('re_type_password');
			$formEdit->setAction('edit');
			$formEdit->setMethod('post');
			$formEdit->getElement('submit')->setLabel('Cập nhật');
			
			 
			$formEdit->getElement('hoten')->setValue($defaultNamespace->user->u_hoten);
			$formEdit->getElement('email')->setValue($defaultNamespace->user->u_email);
			$formEdit->getElement('ngaysinh')->setValue($defaultNamespace->user->u_ngaysinh);
			$formEdit->getElement('diachi')->setValue($defaultNamespace->user->u_diachi);
			$formEdit->getElement('dienthoai')->setValue($defaultNamespace->user->u_dienthoai);
			 
			$mdlUseruserModel = new Model_User(); 
			if ($this->_request->isPost()) {
				//echo "co post duoc khong";
				if ($_POST['hoten']&&$_POST['dienthoai']&&$_POST['diachi']&&$_POST['email']) {
					$defaultNamespace->user=$mdlUseruserModel->updateUser($_POST['hoten'],$_POST['ngaysinh'],
						$_POST['dienthoai'],$_POST['diachi'],
						$_POST['email']);
					$formEdit->getElement('hoten')->setValue($defaultNamespace->user->u_hoten);
					$formEdit->getElement('email')->setValue($defaultNamespace->user->u_email);
					$formEdit->getElement('ngaysinh')->setValue($defaultNamespace->user->u_ngaysinh);
					$formEdit->getElement('diachi')->setValue($defaultNamespace->user->u_diachi);
					$formEdit->getElement('dienthoai')->setValue($defaultNamespace->user->u_dienthoai);	
					/*
					$auth = Zend_Auth::getInstance();
					$storage = $auth->getStorage();
					$storage->write($authAdapter->getResultRowObject(
							array('u_hoten' , 'u_ngaysinh','u_dienthoai','u_diachi' , 'u_email', 'u_password','u_role')));
					*/
						
					$defaultNamespace->name = $_POST['hoten'];

					$this->view->message ="Thông tin đã được cập nhật";
					 
				}else $this->view->error = "Các trường gắn * không được để trống";
			}
			$this->view->form = $formEdit;
		}else{ 
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}	
	}
	
	public function passwordAction ()
	{ 	
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		
		if($defaultNamespace->name){
			$passwordForm = new Form_UserForm();
			
			$passwordForm->setAction('password');
			$passwordForm->removeElement('hoten');
			$passwordForm->removeElement('ngaysinh');
			$passwordForm->removeElement('dienthoai');
			$passwordForm->removeElement('diachi');
			$passwordForm->removeElement('submit');
			
			$passwordForm->getElement('password')->setLabel('Current Password');
			$passwordForm->getElement('re_type_password')->setLabel('New Password');
			
			$r_type_password = $passwordForm->createElement('password', 'r_type_password');
			$r_type_password->setLabel('Re_Type Password: ');
			$r_type_password->setAttrib('size', 30);
			$r_type_password->setRequired('true');
			$r_type_password->setDecorators(array('ViewHelper',
				array(array('data' => 'HtmlTag'),  array('tag' =>'td','class'=>'element')),
				array('Label', array('tag' => 'td')),
				array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
			));
			
			$passwordForm->addElement($r_type_password);
			$passwordForm->addElement('submit', 'submit', array('label' => 'Cập nhật'));
			/*$passwordForm->addElement('submit', 'submit', array('label' => 'Cập nhật',
									'decorators' => array('ViewHelper',array('HtmlTag', array('tag' => 'div', 'id' => 'form-submit')))));
			*/
			$passwordForm->setMethod('post');
			$userModel = new Model_User();
			if ($this->_request->isPost()) { 
				if ($_POST['re_type_password']&&$_POST['r_type_password']&&$_POST['password']){
					if ($_POST['re_type_password'] == $_POST['r_type_password']) {
						$userModel->updatePassword($_POST['email'],$_POST['re_type_password']);
						$this->view->message="Password đã được cập nhật";
						//return $this->_forward('success');
					}else {
						$this->view->erorr="The password you entered is incorrect. Please enter the correct password.";
					}
				}else $this->view->erorr = "Các trường gắn * không được để trống";	
			}
			$this->view->form = $passwordForm;
		}else {
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}
	}
	public function loginAction ()
	{
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		
		if($defaultNamespace->name){
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}else{
			$userForm = new Form_UserForm();
			
			$userForm->setAction('login');
			$userForm->removeElement('hoten');
			$userForm->removeElement('diachi');
			$userForm->removeElement('dienthoai');
			$userForm->removeElement('re_type_password');
			$userForm->removeElement('ngaysinh');
			$userForm->getElement('email')->setAttrib('size',20);
			$userForm->getElement('password')->setAttrib('size',20);
			$userForm->getElement('submit')->setLabel('Đăng nhập');
			if ($this->_request->isPost() && $_POST['email'] && $_POST['password']) {
				$data = array("email"=>$_POST['email'],"password"=>$_POST['password']);
				//var_dump ($data);
				
				//set up the auth adapter
				// get the default db adapter
				$db = Zend_Db_Table::getDefaultAdapter();
				//create the auth adapter
				$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'user','u_email', 'u_password');
				//set the username and password
				$authAdapter->setIdentity($data['email']);
				$authAdapter->setCredential(md5($data['password']));
				//authenticate
				$result = $authAdapter->authenticate();	
				//var_dump($result);
				if ($result->isValid()) {
					// store the username, first and last names of the user
					$auth = Zend_Auth::getInstance();
					$storage = $auth->getStorage();
					//echo $auth->getIdentity()->u_role;
					$storage->write($authAdapter->getResultRowObject(
						array('u_id','u_hoten' , 'u_ngaysinh','u_dienthoai','u_diachi' , 'u_email', 'u_password','u_role')));
					$role = $auth->getIdentity()->u_role;
					$defaultNamespace->user = $auth->getIdentity();
					
					switch ($role){
					case 'customer':
						//return $this->_forward('index');
						$defaultNamespace->name = $auth->getIdentity()->u_hoten;
						if($defaultNamespace->checkout){
							$defaultNamespace->step = 1;
							header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/checkout');
						}else{
							header('Location: http://shoppingcartcnpm.orgfree.com');
						}
						break;
					case 'admin':
						header('Location: http://shoppingcartcnpm.orgfree.com/index.php/Longadmin/admin/home');
						break;
					case 'store_owner':
						header('Location: http://shoppingcartcnpm.orgfree.com/index.php/Longadmin/admin/home');
						break;
					case 'vip_customer':
						//return $this->_forward('index');
						$defaultNamespace->name = $auth->getIdentity()->u_hoten;
						if($defaultNamespace->checkout){
							$defaultNamespace->step = 1;
							header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/checkout');
						}else{
							header('Location: http://shoppingcartcnpm.orgfree.com');
						}
						break;
					case 'always_customer':
						//return $this->_forward('index');
						$defaultNamespace->name = $auth->getIdentity()->u_hoten;
						if($defaultNamespace->checkout){
							$defaultNamespace->step = 1;
							header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/checkout');
						}else{
							header('Location: http://shoppingcartcnpm.orgfree.com');
						}
						break;		
					}	
				} else {
					$this->view->loginMessage = "Xin lỗi, password hoặc email đăng nhập không đúng";
				}
				
			}
			$this->view->form = $userForm;
		}
	}
	public function menuAction()
	{
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
	}
	
	public function indexAction()
	{
		$auth = Zend_Auth::getInstance();

		if($auth->hasIdentity()) {
			$this->view->identity = $auth->getIdentity (); 
		}
	}	
	public function logoutAction()
	{
		$authAdapter = Zend_Auth::getInstance();
		$authAdapter->clearIdentity();
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		$defaultNamespace->name=null;
		$defaultNamespace->user = null;
		header('Location: http://shoppingcartcnpm.orgfree.com');
		
	}
	
	public function listAction ()
	{
		$currentUsers = Model_User::getUsers();
		if ($currentUsers->count() > 0) {
			$this->view->users = $currentUsers;
	
		} else {
			$this->view->users = null;
		}
	}
	public function accountAction(){
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		$this->view->user = $defaultNamespace->user;
		if($defaultNamespace->user){
			$modelMessages = new Model_Messages();
			$newMessages = $modelMessages->getNewMessagesByUid($defaultNamespace->user->u_id,0);
			$this->view->countNew = count($newMessages);
		}
	}
	public function accountinfoAction(){
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		$this->view->user = $defaultNamespace->user;
	}
}